package com.qf.controller;

import com.qf.config.DelayedConfig;
import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class DelayedController {

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @GetMapping("/delayed/{msg}/{delayed}")
    public String delayed(@PathVariable String msg,@PathVariable Integer delayed){
        System.out.println("生产者发送" + msg + "消息:" + System.currentTimeMillis());
        rabbitTemplate.convertAndSend(DelayedConfig.DELAYED_EXCHANGE, "", msg, new MessagePostProcessor() {
            @Override
            public Message postProcessMessage(Message message) throws AmqpException {
                message.getMessageProperties().setDelay(delayed);
                return message;
            }
        });
        return "ok";
    }

}
